package com.zbf.common;

import com.zbf.ex.LoginFailException;
import com.zbf.ex.NoAuthException;
import com.zbf.ex.NoLoginException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/1  15:08
 * 描述: 做全局的异常处理
 */
@RestControllerAdvice
public class MyExceptionHandler {

    /**
     * 作者: LCG
     * 日期: 2021/6/1  15:18
     * 描述: 登录的异常处理
     * @Param [loginException]
     * @Return com.zbf.common.ResponseResult
     */
   @ExceptionHandler(LoginFailException.class)
   public ResponseResult loginFailExceptionHandler(LoginFailException loginException){

       String message = loginException.getMessage();

       ResponseResult responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_LOGIN_FAIL.getCode())
               .setMsg(ResultStatus.AUTH_LOGIN_FAIL.getMsg())
               .setResult(message);

       return responseResult;
   }


   /**
    * 作者: LCG
    * 日期: 2021/6/1  15:19
    * 描述: 无权限异常
    * @Param [noAuthException]
    * @Return com.zbf.common.ResponseResult
    */
   @ExceptionHandler(NoAuthException.class)
   public ResponseResult noAuthExceptionHadler(NoAuthException noAuthException){

       ResponseResult responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_NO_AUTH.getCode())
               .setMsg(ResultStatus.AUTH_NO_AUTH.getMsg())
               .setResult(noAuthException.getMessage());

       return responseResult;

   }


   /**
    * 作者: LCG
    * 日期: 2021/6/1  16:03
    * 描述: 未登录异常处理
    * @Param [noLoginException]
    * @Return com.zbf.common.ResponseResult
    */
   @ExceptionHandler(NoLoginException.class)
   public ResponseResult noLoginExceptionHadler(NoLoginException noLoginException){

       ResponseResult responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_NO_LOGIN.getCode())
               .setMsg(ResultStatus.AUTH_NO_LOGIN.getMsg())
               .setResult(noLoginException.getMessage());

       return responseResult;

   }


    @ExceptionHandler(RuntimeException.class)
    public ResponseResult noLoginExceptionHadler(RuntimeException runtimeException){

        ResponseResult responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_FAIL.getCode())
                .setMsg(ResultStatus.AUTH_FAIL.getMsg())
                .setResult(runtimeException.getMessage());

        return responseResult;

    }


}
